package com.justnow.offer;


import java.util.*;

/**
 * @author justnow
 * Created on 2020-09-09
 * Description
 */
public class Solution32 {
    public static void main(String[] args) {
        String[] products = {"mobile","mouse","moneypot","monitor","mousepad"};
        String searchWord = "mouse";
        Solution32 solution32 = new Solution32();
        List<List<String>> lists = solution32.suggestedProducts(products, searchWord);
        System.out.println(lists);
    }

    public List<List<String>> suggestedProducts(String[] products, String searchWord) {
        List<List<String>> list = new ArrayList<>();
        Arrays.sort(products); //直接进行排序
        int count = 1;
        while (count < searchWord.length() + 1) {
            List<String> temp = new ArrayList<>();
            String substring = searchWord.substring(0, count);
            int matchNumber = 0;
            for (String product : products) {
                if (matchNumber == 3){
                    break;
                }
                if (product.length() >= count) {
                    String subProduct = product.substring(0, count);
                    if (substring.equals(subProduct)) {
                        matchNumber++;
                        temp.add(product);
                    }
                }
            }
            list.add(temp);
            count++;
        }
        return list;
    }
}
